/********************************************
 * 功能说明: 
 * 模块名称: 
 * 系统名称: 
 * 软件版权: 北京银杉金服科技有限公司
 * 系统版本: 1.0.0
 * 开发人员: zhangfb
 * 开发时间: 2018/9/8 20:26
 * 审核人员: 
 * 相关文档: 
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.hyacinth.security;

import com.hyacinth.annotation.LogAnnotation;
import com.hyacinth.annotation.NoNeedAccessAuthentication;
import com.hyacinth.constant.GlobalConstant;
import com.hyacinth.constant.LogTypeEnum;
import com.hyacinth.util.JsonMapper;
import com.hyacinth.util.RequestUtil;
import com.hyacinth.util.ThreadLocalMap;
import com.hyacinth.wapper.WrapMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.util.WebUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhangfb
 * @version 1.0.0.1
 * @since JDK 1.8
 */
@Slf4j
@Component
public class CustomAuthSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{

    @Override
    @LogAnnotation(logType = LogTypeEnum.LOGIN_LOG, isSaveReqData = true, isSaveRespData = true, desc = "用户登陆日志")
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        SecurityUser userDetails =(SecurityUser)authentication.getPrincipal();
        log.info("管理员 "+userDetails.getName()+" 登录成功，IP:"+ RequestUtil.getRemoteAddr(request));
        // 记录登陆日志 TODO
        //super.onAuthenticationSuccess(request, response, authentication);
        // 将登录用户放到redis中
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(JsonMapper.toJsonString(authentication));
    }
}
